# @file    : main_window
# @time    : 2024/4/18
# @author  : yongpeng.yao
# @desc    :
from kivy import Logger
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.properties import OptionProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.popup import Popup

from common import KvLoad, get_devices, write_conf_by_option
from common.local_conf import get_conf_device
from ui.audio_player.interact_track import InteractPanel
from ui.audio_player.music_track.play_row import PlayRow


class MainWindow(BoxLayout):
    # 主音轨播放状态
    primary_pay_state = OptionProperty('stop', options=["stop", "play", "pause"])
    # 互动音轨播放状态
    interact_pay_state = OptionProperty('stop', options=["stop", "play", "pause"])

    def __init__(self, **kwargs):
        self.popup = None
        KvLoad.load(__file__)
        super(MainWindow, self).__init__(**kwargs)
        Clock.schedule_once(lambda x: self.set_window_top())

        self.init_ui()

        Window.bind(on_request_close=self.on_request_close)

    @staticmethod
    def set_window_top():
        Window.raise_window()

    def init_ui(self):
        self.ids.id_content.add_widget(PlayRow(track_type='primary'))
        self.ids.id_content.add_widget(InteractPanel())
        self.ids.id_content.add_widget(PlayRow(track_type='secondary'))
        self.ids.id_content.add_widget(PlayRow(track_type='secondary'))
        self.ids.id_content.add_widget(BoxLayout())
        if device := get_conf_device():
            self.ids.id_button_dropdown.text = device
        self.ids.id_button_dropdown.bind(on_release=self.init_dropdown)

    def init_dropdown(self, widget):
        dropdown = DropDown(on_select=self.on_select_audio_device)

        devices = get_devices()
        Logger.info(f'devices {devices}')
        for index, device in enumerate(devices):
            # 增加选择音频输出按钮
            dropdown.add_widget(Button(text=device, size_hint_y=None, height=widget.height,
                                       font_size=widget.font_size,
                                       on_release=lambda bt: dropdown.select(bt.text)))

        dropdown.open(widget)

    def on_select_audio_device(self, widget, device):
        _ = widget
        if get_conf_device() != device:
            write_conf_by_option('audio_setting', 'device', device)
            self.ids.id_button_dropdown.text = device
            self.ids.id_restart_effect_tip.text = '重启后生效'

    def on_request_close(self, *args, **kwargs):
        _, _ = args, kwargs
        if self.popup is None:
            self.open_request_popup()
        return True

    def open_request_popup(self):
        def on_yes(instance):
            Window.on_close()  # 停止应用程序

        def on_no(instance):
            self.popup.dismiss()
            self.popup = None

        content = BoxLayout(padding=40, spacing=40)
        yes_btn = Button(text='退出', size_hint_y=None, height=50)
        yes_btn.bind(on_release=on_yes)
        no_btn = Button(text='取消', size_hint_y=None, height=50)
        no_btn.bind(on_release=on_no)
        content.add_widget(yes_btn)
        content.add_widget(no_btn)

        self.popup = Popup(title='确定要退出吗？', content=content,
                           size_hint=(None, None), size=(460, 360), auto_dismiss=False)
        self.popup.open()
